const router = require('express').Router();
const { check, validationResult } = require('express-validator');
const teacherControllers = require('../../controllers/teacherControllers');

// 获取当前登录教师信息
router.get('/', async function (req, res) {
  const { id } = req.auth;
  const data = await teacherControllers.selectById(id);
  res.sendResult(data, 200, '请求成功');
});

// 更新当前登录教师的基础信息（这里只更新姓名，保持最小实现）
router.put(
  '/',
  [
    check('teacher_name').notEmpty().withMessage('teacher_name不能为空')
  ],
  async function (req, res) {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }
    const { id } = req.auth;
    const current = await teacherControllers.selectById(id);
    if (!current) return res.sendResult(null, 404, '教师不存在');
    // 仅更新姓名，沿用现有 updateByDid 方法
    const ok = await teacherControllers.updateByDid(
      current.teacher_department_id,
      current.teacher_number,
      req.body.teacher_name,
      id,
      0
    );
    res.sendResult(ok, 202, '更新成功');
  }
);

module.exports = router;
